#include <QDebug>

#include "cvactivitymanager.h"

CVActivityManager::CVActivityManager()
{
}

void CVActivityManager::getActivities(QMap<QString, QString> options)
{
    QString userId = options.value("userId");
    QString limit = options.value("limit");
    QString offset = options.value("offset");
    QString filterId = options.value("filterId");
    QString rid = options.value("rid");
    QString guids = options.value("guids");
    if (limit.isEmpty())
    {
        limit = "20";
    }
    if (offset.isEmpty())
    {
        offset = "0";
    }

    CVSession *session = CVSession::get();
    QString sessionId = session->getSessionId();
    qDebug() << "Session ID: " << sessionId;
    if (!sessionId.isNull() && !sessionId.isEmpty())
    {
        QString network = session->getCurrentNetwork()->getUrl();
        qDebug() << "Network: " << network;
        if (!userId.isNull() && !userId.isEmpty())
        {
            QUrl url = CVUtil::getBaseUrl("mobile.get_updates", network);
            url.addQueryItem("user_id", userId);
            url.addQueryItem("limit", limit);
            url.addQueryItem("offset", offset);
            url.addQueryItem("show_all", "5");
            url.addQueryItem("lang", "en");
            url.addQueryItem("rid", rid);
            url.addQueryItem("guids", guids);
            url.addQueryItem("filter_id", filterId);
            CVHttp *http = new CVHttp(url);
            //http->setDebug(true);
            connect(http, SIGNAL(finished(QScriptValue,QString)), this, SLOT(parseData(QScriptValue,QString)));
        }
    }
}

void CVActivityManager::parseData(QScriptValue json, const QString &message)
{
    qDebug() << "==CVActivityManager::parseData==";
    QList<CVActivity *> list;
    if (!json.isNull() && !json.isError())
    {
        QScriptValueIterator it(json);
        while (it.hasNext())
        {
            it.next();
            QScriptValue value = it.value();
            if (value.property("river_id").toString().isEmpty())
            {
                continue;
            }
            CVActivity *activity = new CVActivity();
            activity->setKey(value.property("key").toString());
            activity->setMessage(value.property("message").toString());
            activity->setTitle(value.property("title").toString());
            activity->setActionType(value.property("action_type").toString());
            activity->setDescription(value.property("description").toString());
            activity->setObjectId(value.property("object_id").toString());
            activity->setObjectName(value.property("object_name").toString());
            activity->setObjectType(value.property("object_type").toString());
            activity->setSubjectIcon(value.property("subject_icon").toString());
            activity->setCommentsOn(value.property("comments_on").toString());
            activity->setCommentsCount(value.property("comments_count").toString());
            activity->setMimeType(value.property("mime_type").toString());
            activity->setPosted(value.property("posted").toString());
            activity->setRid(value.property("rid").toString());
            activity->setRiverId(value.property("river_id").toString());
            //qDebug() << "River ID:" << value.property("river_id").toString();
            if (!value.property("file_version").isNull())
            {
                activity->setFileVersion(value.property("file_version").toString());
            }
            if (!value.property("is_downloadable").isNull() && value.property("is_downloadable").isBool())
            {
                activity->setIsDownloadable(value.property("is_downloadable").toBool());
            }
            if (!value.property("object_small_icon").isNull())
            {
                activity->setObjectSmallIcon(value.property("object_small_icon").toString());
            }
            if (!value.property("params").isNull())
            {
                QScriptValue params = value.property("params");
                if (params.isArray())
                {
                    QScriptValueIterator itor(params);
                    QList<CVActivityParam*> *plist = new QList<CVActivityParam*>();
                    plist->begin();
                    while(itor.hasNext()) {
                        itor.next();
                        //qDebug() << itor.value().toString();
                        if (itor.value().isObject())
                        {
                            QString prop_type = itor.value().property("type").toString();
                            QString prop_name = "";
                            QString prop_guid = "";
                            QString prop_subtype = "";
                            if (itor.value().property("type").toString() == "plaintext")
                            {
                                prop_name = itor.value().property("text").toString();
                            }
                            else
                            {
                                prop_name = itor.value().property("name").toString();
                                prop_guid = itor.value().property("guid").toString();
                                prop_subtype = itor.value().property("subtype").toString();
                            }

                            CVActivityParam *param =
                                    new CVActivityParam(prop_type, prop_name,
                                                        prop_guid, prop_subtype);
                            plist->append(param);

                        }
                    }
                    plist->end();
                    activity->setParams(*plist);
                }

            }
            list.append(activity);
        }
    }

    emit handActivities(list, message);
}
